﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IBuy.Entity;
using System.Collections;
using IBuy.DataAccessLayer;

namespace IBuy.Forms
{
    public partial class DiscountForm : Form
    {
        private DatabaseFactory databaseFoctory;
        private DisCount discount;
        private Product goods;
        private Agency agency;

        public DiscountForm()
        {
            InitializeComponent();
            databaseFoctory = new OledbDatabaseFactory();
            discount = new DisCount();
            goods = new Product();
            agency = new Agency();
        }

        private void DiscountForm_Load(object sender, EventArgs e)
        {
            SetDataSource();
        }
        private void SetDataSource()
        {
            DataSet dataset = discount.GetAllDiscountDataSet();
            this.dgvAgency.DataSource = dataset.Tables[0].DefaultView;
            this.cmbGoodsName.DataSource = goods.GetNameAndIdOFAllGoods().Tables[0].DefaultView;
            this.cmbGoodsName.DisplayMember = "NAME";
            this.cmbGoodsName.ValueMember = "GOODSID";
            this.cmbAgencyName.DataSource = agency.GetNameAndIdOfAllAgency().Tables[0].DefaultView;
            this.cmbAgencyName.DisplayMember = "AGENCYNAME";
            this.cmbAgencyName.ValueMember = "AGENCYID";
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearDiscountContent();
        }
        private void ClearDiscountContent()
        {
            this.cmbAgencyName.SelectedIndex = 0;
            this.cmbGoodsName.SelectedIndex = 0;
            this.txtDiscount.Text = "";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Hashtable table = new Hashtable();
            table.Add("AGENCYID", this.cmbAgencyName.SelectedValue.ToString());
            table.Add("GOODSID", this.cmbGoodsName.SelectedValue.ToString());
            table.Add("DISCOUNT", this.txtDiscount.Text.Trim());
            discount.InsertAgencyDiscount(table);
            SetDataSource();
            ClearDiscountContent();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            
            if (this.cmbAgencyName.Text.Trim().Equals(""))
            {
                MessageBox.Show("请确定待修改的代理商的名称不为空！");
                return;
            }
            string where =" where AGENCYID=" + this.cmbAgencyName.SelectedValue.ToString() + " and GOODSID="+this.cmbGoodsName.SelectedValue.ToString();
            Hashtable table = new Hashtable();
            table.Add("DISCOUNT", this.txtDiscount.Text.Trim());
            discount.UpdateAgencyDiscount(table, where);
            SetDataSource();
            ClearDiscountContent();
        }

        private void dgvAgency_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = this.dgvAgency.Rows[e.RowIndex];
            this.cmbAgencyName.Text = row.Cells["AGENCYNAME"].Value.ToString();
            this.cmbGoodsName.Text = row.Cells["GOODSNAME"].Value.ToString();
            this.txtDiscount.Text = row.Cells["DISCOUNT"].Value.ToString();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.cmbAgencyName.Text.Trim().Equals("")||this.cmbGoodsName.Text.Trim().Equals(""))
            {
                MessageBox.Show("请确定待删除的代理商名称不为空！");
                return;
            }
            string where = " where AGENCYID=" + this.cmbAgencyName.SelectedValue.ToString() + " and GOODSID=" + this.cmbGoodsName.SelectedValue.ToString();
            discount.DeleteAgencyDiscount(where);
            SetDataSource();
            ClearDiscountContent();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            DataSet dataset = discount.GetDiscountDataSetByName(this.txtCondition.Text);
            this.dgvAgency.DataSource = dataset.Tables[0].DefaultView;
        }
    }
}
